package com.jieyang.oa.controller.person_manager;

import com.google.gson.Gson;
import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.entity.Dept;
import com.jieyang.oa.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: 部门管理
 * @date 2021/8/15 10:33
 */
@WebServlet("/deptServlet")
public class DeptServlet extends BaseServlet {

    @Autowired
    private DeptService deptService;

    /**
     * @Author 2927624517@qq.com
     * @Description 新增部门
     * @Date 2021/8/28 10:47
     * @Param [req, resp]
     * @return void
    **/
    public void deptAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Long deptNo = Long.valueOf(req.getParameter("deptNo"));
        String deptName = req.getParameter("deptName");
        String location = req.getParameter("location");
        Dept dept = new Dept();
        dept.setDeptNo(deptNo);
        dept.setDeptName(deptName);
        dept.setLocation(location);
        Integer insert = deptService.insert(dept);
        if (insert == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询所有部门
     * @Date 2021/8/28 10:47
     * @Param [req, resp]
     * @return void
    **/
    public void deptList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        List<Dept> depts = deptService.selectList();
        resp.getWriter().write(new Gson().toJson(depts));
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 删除部门
     * @Date 2021/8/28 10:48
     * @Param [req, resp]
     * @return void
    **/
    public void deleteDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer deptNo = Integer.valueOf(req.getParameter("deptNo"));
        Integer delete = deptService.delete(deptNo);
        if (delete == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 更新部门（返回部门信息，让用户选择修改内容）
     * @Date 2021/8/28 10:48
     * @Param [req, resp]
     * @return void
    **/
    public void updateDept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer deptNo = Integer.valueOf(req.getParameter("deptNo"));
        Dept dept = deptService.selectByDeptNo(deptNo);

        req.setAttribute("dept", dept);
        req.getRequestDispatcher("personManager/deptUpdate.jsp").forward(req, resp);
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 更新部门（确认更新用户选择的内容）
     * @Date 2021/8/28 10:49
     * @Param [req, resp]
     * @return void
    **/
    public void deptConfirmUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Long deptNo = Long.valueOf(req.getParameter("deptNo"));
        String deptName = req.getParameter("deptName");
        String location = req.getParameter("location");
        Dept dept = new Dept();
        dept.setDeptNo(deptNo);
        dept.setDeptName(deptName);
        dept.setLocation(location);
        Integer update = deptService.update(dept);

        if (update == 0) {
            resp.getWriter().write("false");
        } else {
            resp.getWriter().write("true");
        }
    }
}
